package edu.itcs6112.za.service.jpa;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.google.common.collect.Lists;

import edu.itcs6112.za.domain.Customer;
import edu.itcs6112.za.repository.CustomerRepository;
import edu.itcs6112.za.service.CustomerService;

@Service("customerService")
@Repository
@Transactional
public class CustomerServiceImpl implements CustomerService {

	@Autowired
	private CustomerRepository customerRepository;

	@Transactional(readOnly=true)
	public List<Customer> findAll() {
		return Lists.newArrayList(customerRepository.findAll());
	}

	@Transactional(readOnly=true)
	public Customer findById(Long id) {
		return customerRepository.findOne(id);
	}

	public Customer save(Customer customer) {
		return customerRepository.save(customer);
	}

	@Transactional(readOnly=true)
	public Page<Customer> findAllByPage(Pageable pageable) {
		return customerRepository.findAll(pageable);
	}	
	
	@Transactional(readOnly=true)
	public Customer findByUsername(String username){
		return customerRepository.findByUserName(username);
	}
	
}
